package com.xdap.keymen.utils;

import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Slf4j
public class DateTimeUtils {

    public static Long date2TimeStamp(String date_str, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.parse(date_str).getTime();
        } catch (Exception e) {
           log.error(e.getMessage(), e);
        }
        return 0L;
    }
    public static Date dateFormat(String datetime,String format){
        SimpleDateFormat ft = new SimpleDateFormat(format);
        Date date;
        try {
            date = ft.parse(datetime);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return date;
    }


    public static long between(Date start,Date end){
        long diffInMillies = Math.abs(end.getTime() - start.getTime());
        return TimeUnit.DAYS.convert(diffInMillies, TimeUnit.MILLISECONDS);
    }
}
